package com.mywork.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ciko.code.base.BaseActicity;
import com.ciko.code.http.BaseRequest;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.Req.SelUserPostInfoListEty;
import com.ciko.code.http.Req.SelUserPostInfoListReq;
import com.ciko.code.utils.Util;
import com.mywork.R;
import com.mywork.WorkUIKit;
import com.mywork.adapter.GeneralAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sihailin on 15/12/28.
 */
public class ChooseSupActivity extends BaseActicity implements AdapterView.OnItemClickListener{
    private ListView lv_general;
    private GeneralAdapter mGeneralAdapter;
    private List<SelUserPostInfoListEty> UserPostlist = new ArrayList<>();
    @Override
    protected void initTopBar() {
        setTitle("选择接收计划人员");
        enableNavLeftImg();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_choose_sup);
    }

    @Override
    protected void initView() {
        lv_general = (ListView)findViewById(R.id.lv_choose_sup);
        lv_general.setOnItemClickListener(this);
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
        mGeneralAdapter = new GeneralAdapter(ChooseSupActivity.this, UserPostlist);
        lv_general.setAdapter(mGeneralAdapter);
        if (WorkUIKit.getUser().getBranchNo().equals(""))
        {
            Util.showToast(this, R.string.params_less_tip);
            return;
        }
        doRequestHttp(new SelUserPostInfoListReq(WorkUIKit.getUser().getBranchNo(), WorkUIKit.getUser().getDepartmentNo(), WorkUIKit.getUser().getUserNo(), null, 100));
    }

    @Override
    protected void initListener() {

    }

    protected void doRequestHttp(BaseRequest request) {
        HttpClient.getInstance(ChooseSupActivity.this).post(request, this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        SelUserPostInfoListEty item = mGeneralAdapter.getItem(i);
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("rusult",item);
        intent.putExtras(bundle);
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        switch (tag){
            case "SelUserPostInfoListReq":
                List<SelUserPostInfoListEty> listResult2 = (List<SelUserPostInfoListEty>) response.getReturnObject();
                UserPostlist.clear();
                UserPostlist.addAll(listResult2);
                mGeneralAdapter.notifyDataSetChanged();
                break;
        }

    }
}
